#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>

#define ulong unsigned long 	// For KISS pseudo random numbers generator
#define RAND_MAX_KISS ((unsigned long) 4294967295) // idem

static ulong kiss_x = 1;
static ulong kiss_y = 2;
static ulong kiss_z = 4;
static ulong kiss_w = 8;

static ulong kiss_carry = 0;
static ulong kiss_k;
static ulong kiss_m;


void seed_rand_kiss(ulong seed)
{
    kiss_x = seed | 1;

    kiss_y = seed | 2;
    kiss_z = seed | 4;

    kiss_w = seed | 8;
    kiss_carry = 0;
}

//seed_rand_kiss(1); // Initialise KISS


ulong rand_kiss()
{
    kiss_x = kiss_x * 69069 + 1;
    kiss_y ^= kiss_y << 13;


    kiss_y ^= kiss_y >> 17;
    kiss_y ^= kiss_y << 5;
    kiss_k = (kiss_z >> 2) + (kiss_w >> 3) + (kiss_carry >> 2);
    kiss_m = kiss_w + kiss_w + kiss_z + kiss_carry;
    kiss_z = kiss_w;
    kiss_w = kiss_m;
    kiss_carry = kiss_k >> 30;
    return kiss_x + kiss_y + kiss_w;
}

double	alea(double a,double b)
{	// rand number (uniform distribution) in [a b]

double 	r;
 r=(double)rand_kiss()/RAND_MAX_KISS;
// r=(double)rand(); r=r/RAND_MAX;
return a+r*(b-a);
}

int	alea_integer(int a,int b)
{// Integer rand number in [a b]
int	ir;
double 	r;
r=alea(0,1); ir=(int)(a+r*(b+1-a)); if (ir>b) ir=b;
return ir;
}

/*main()
{
    int d,i;
    double e;

    FILE *f1;
    f1=fopen("rand_kiss.txt","w");

	for(i=0;i<500;i++)
    {

       d = alea_integer(0,100);
	   e=alea(0.0,1.0);
       printf("%d----%lf\n",d,e);
       fprintf(f1,"%d----%lf\n",d,e);
       //if(d==1 ) cnt++;

    }

    //getch();
}*/